from db import db, Log
from flask import request


def log_request():
    """
    将请求记入数据库日志
    :return:
    """
    log = Log()
    log.method = request.method
    log.addr = request.headers.get("X-Real-IP") or request.remote_addr
    log.ua = request.headers.get("User-Agent") or None
    log.url = request.url
    log.data = request.data.decode('utf8') or None
    db.session.add(log)
    db.session.commit()
